<?php
/**
 * File containing the RandomTelephoneNumberProcessor
 *
 * PHP Version 5
 *
 * @category Processors
 * @package  Processors
 * @author   Martin Schlecht <herrlollinger@gmail.com>
 * @license  MIT License
 * @link     http://code.google.com/p/universal-test-data
 */

/**
 * Processor class "RandomTelephoneNumberProcessor"
 *
 * Processor which is used to process tags which implements random telephone numbers within the text.
 *
 * @category   Processors
 * @package    Processors
 * @subpackage RandomTelephoneNumberProcessors
 * @author     Martin Schlecht <herrlollinger@gmail.com>
 * @copyright  see @author
 * @license    MIT License
 * @link       http://code.google.com/p/universal-test-data
 *
 * The followings are the available attributes of the processor class
 * @property string $text, the text which should be processed
 */
class RandomTelephoneNumberProcessor
{
	private $text;
	
	/**
	 * The processor adds random generated telephone numbers and repeats the numbers n times
	 *
	 * @param String $text, the text to process
	 *
	 * @return String
	 */
	public function process($text) {
		$this->text = $text;
		
		preg_match_all("#\s*{(.*?)}#si", $this->text, $matches);
		
		foreach ($matches[1] AS $m) {
			$string = "{" . $m . "}";
			$m = explode(":", $m);
			
			/**
			 * {telephoneNumberCountry:DE}
			 * Random telephone number with german layout
			 */
			if ($m[0] == "telephoneNumberCountry") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || $parameter[0] == "") {
					$parameter[0] = "DE";
				}
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 1;
				}
			
				$this->text = str_replace($string, $this->manageRepeation("+" . $this->getCountryPrefix($parameter[0]) . $this->getTelephoneNumber(), $string, $parameter[1]), $this->text);
			}
		}
		
		return $this->text;
	}
	
	/**
	 * Returns a random telephone number with default lenght 8
	 * 
	 * @return int
	 */
	public function getTelephoneNumber($lenght = 8) {
		$randomNumberProcessor = new RandomNumberProcessor();
		return $randomNumberProcessor->numberWithFixedLenght($lenght);
	}
	
	/**
	 * Returns the country codes for a given domain
	 * 
	 * @return String
	 */
	public function getCountryPrefix($domain) {
		$number = 0;
		$countryCodes = $this->getCountryCodes();
		$domain .= "";
		
		if (array_key_exists($domain, $countryCodes)) {
			$number = $countryCodes[$domain];
		} else {
			$number = "[No valid country code]";
		}
		
		return $number;
	}
	
	/**
	 * Manages the repeation of values
	 * 
	 * @param String $value,  the value which should be replaced
	 * @param String $string, the string for the cockie
	 * @param String $times,  the number of times the value should be repeated
	 * 
	 * @return String
	 */
	public function manageRepeation($value, $string, $times) {
		$replacement = "";
		
		if (Yii::app()->session[$string . "_times"] >= $times || !isset(Yii::app()->session[$string . "_times"])) {
			Yii::app()->session[$string . "_replacement"] = $value;
			Yii::app()->session[$string . "_times"] = 1;
							
			$replacement = $value;
		} else {
			$number = Yii::app()->session[$string . "_replacement"];
			$times = Yii::app()->session[$string . "_times"] + 1;
			
			Yii::app()->session[$string . "_times"] = $times;
			
			$replacement = $number;
		}
		
		Yii::app()->session['resetManager']->add($string . "_replacement");
		Yii::app()->session['resetManager']->add($string . "_times");
		
		return $replacement;
	}
	
	/**
	 * Returns the documentation
	 *
	 * @return String
	 */
	 public function documentation() {
		return array(
			array('{telephoneNumberCountry:DOMAIN}', 'Random telephone number with layout for domain'),
			array('{telephoneNumberCountry:DOMAIN, times}', 'Random telephone number with layout for domain, n times repeated'), 
		);
	 }
	 
	/**
	 * Prints the documentation
	 *
	 * @return string
	 */
	public function print_documentation() {
		foreach ($this->documentation() AS $docu) {
			echo '<span style="font-family:monospace;font-size:11px">' . $docu[0] . '</span><br/>' . $docu[1] . '<br/>';
		}
	}
	
	/**
	 * Returns a array which contains a list of country codes. The codes are matched via domain names.
	 *
	 * @return array
	 */
	public function getCountryCodes() {
		return array(
			"US" => "1",
			"CA" => "1",
			"DE" => "49",
			"EG" => "20",
			"ZA" => "27",
			"GR" => "30",
			"NL" => "31",
			"BE" => "32",
			"FR" => "33",
			"ES" => "34",
			"PT" => "351",
			"LU" => "352",
			"IE" => "353",
			"IS" => "354",
			"AL" => "355",
			"MT" => "356",
			"CY" => "357",
			"FI" => "358",
			"BG" => "359",
			"HU" => "36",
			"LT" => "370",
			"LV" => "371",
			"EE" => "372",
			"MD" => "373",
			"AM" => "374",
			"BY" => "375",
			"AD" => "376",
			"MC" => "377",
			"SM" => "378",
			"VA" => "379",
			"UA" => "380",
			"RS" => "381",
			"ME" => "382",
			"HR" => "385",
			"SI" => "386",
			"BA" => "387",
			"MK" => "389",
			"IT" => "39",
			"RO" => "40",
			"CH" => "41",
			"CZ" => "420",
			"SK" => "421",
			"LI" => "423",
			"AT" => "43",
			"UK" => "44",
			"DK" => "45",
			"SE" => "46",
			"NO" => "47",
			"PL" => "48",
			"MX" => "52",
			"CU" => "53",
			"AR" => "54",
			"BR" => "55",
			"CL" => "56",
			"CO" => "57",
			"VE" => "58",
			"MY" => "60",
			"AU" => "61",
			"ID" => "62",
			"PH" => "63",
			"NZ" => "64",
			"SG" => "65",
			"TH" => "66",
			"KZ" => "7",
			"RU" => "7",
			"JP" => "81",
			"KR" => "82",
			"VN" => "84",
			"HK" => "852",
			"CN" => "86",
			"BD" => "880",
			"TW" => "886",
			"TR" => "90",
			"PK" => "92",
			"IN" => "91",
			"LK" => "94",
			"AF" => "93",
			"MM" => "95",
			"IQ" => "964",
			"SY" => "963",
			"SA" => "966",
			"YE" => "967",
			"OM" => "968",
			"QA" => "974",
			"IL" => "972",
			"AE" => "971",
			"NP" => "977"
		);
	}
}
?>